package com.example.statistics.common

import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.ActivityInfo
import android.graphics.PorterDuff
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.example.statistics.R
import com.example.statistics.utils.StatusBarUtil
import com.example.statistics.ex.glide
import com.example.statistics.ex.inflater
import kotlinx.android.synthetic.main.activity_base.*
import kotlinx.android.synthetic.main.layout_top_bar.view.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.sdk27.coroutines.onTouch

abstract class BaseActivity : AppCompatActivity() {

    open val isDefaultStatusBar = true
    open val isOrientationPortrait = true
    open val isTotalBarDisplay = true
    abstract val title: String


    @SuppressLint("SourceLockedOrientationActivity")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (isOrientationPortrait) requestedOrientation =
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT //竖屏


    }


    override fun setContentView(layoutResID: Int) {
        initialize()
        val parent = inflater.inflate(R.layout.activity_base, null) as LinearLayout
        val child = inflater.inflate(layoutResID, parent, false)
        parent.addView(child)
        setContentView(parent)

        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN or WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        child.onTouch { v, event ->
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager;
            imm.hideSoftInputFromWindow(v.windowToken, 0)
        }
        if (isDefaultStatusBar) {
            StatusBarUtil.setTransparentForImageViewInFragment(this, null)
            StatusBarUtil.setLightMode(this)
        }
        if (!isTotalBarDisplay) {
            top_bar.visibility = View.GONE
        }
        top_bar.tv_top_bar_title.text = title
        top_bar.iv_top_bar_back.visibility = View.VISIBLE
        top_bar.iv_top_bar_back.onClick {
            onBackPressed()
        }

    }

    fun topBarRightText(text: String, body: () -> Unit) {
        top_bar.tv_top_bar_right.visibility = View.VISIBLE
        top_bar.tv_top_bar_right.text = text
        top_bar.tv_top_bar_right.onClick {
            body()
        }
    }

    fun topBarRightImage(image: Int = R.drawable.ic_blank_add, color: Int? = null, body: () -> Unit) {
        if (color != null) top_bar.iv_top_bar_right.setColorFilter(color, PorterDuff.Mode.SRC_IN)
        top_bar.iv_top_bar_right.visibility = View.VISIBLE
        top_bar.iv_top_bar_right.glide(image)
        top_bar.iv_top_bar_right.onClick {
            body()
        }
    }

    fun topBarOtherImage(image: Int = R.drawable.ic_list_24dp, color: Int? = null, body: () -> Unit) {
        if (color != null) top_bar.iv_top_bar_other.setColorFilter(color, PorterDuff.Mode.SRC_IN)
        top_bar.iv_top_bar_other.visibility = View.VISIBLE
        top_bar.iv_top_bar_other.glide(image)
        top_bar.iv_top_bar_other.onClick {
            body()
        }
    }

    open fun initialize() {}

}